<?php
namespace app\admin\model\article;
use think\Cache;
use think\Model;

class Helps extends Model
{

    protected $name='help';
    protected $autoWriteTimestamp='int';
    protected $createTime='createtime';
    protected $updateTime='updatetime';



    public function getList($is_recycle = false){
        return $this->where('is_recycle', '=', (int)$is_recycle)
            ->where('is_delete', '=', 0)
            ->order(['createtime' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

  public function getdelList(){
      return $this->where('is_delete',1)->order(['createtime' => 'desc'])
          ->paginate(15, false, [
              'query' => request()->request()
          ]);

  }

    /**
     * 详情
     * @param $id
     * @return Category|null
     * @throws \think\exception\DbException
     */
    public static function detail($id)
    {
        return static::get($id);
    }



    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {

        $this->deleteCache();
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        $this->deleteCache();
        return $this->allowField(true)->save($data);
    }

    /**
     * 删除
     * @param $id
     * @return bool|int
     */
    public function remove($id)
    {
        $this->deleteCache();
        return $this->delete($id);
    }

    /**
     * 移入移出回收站
     * @return false|int
     */
    public function recycle($is_recycle = true)
    {
        if($is_recycle==true){
            return $this->save(['is_recycle' =>1,'is_delete'=>1]);
        }else{
            return $this->save(['is_recycle' =>0,'is_delete'=>0]);
        }

    }

    /**
     * 删除缓存
     * @return bool
     */
    private function deleteCache()
    {
        return Cache::rm('base_helps');
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->transaction(function () {
            // 设置当前商城为已删除
            return $this->save(['is_delete' => 1]);
        });
    }

}